#产生随机数
randIndex = (min,max,cnt,except=[])->
	ret = []
	i = 999999
	while ret.length < cnt and max-min>0 and i-- >0
		newRand = Math.floor(Math.random()*(max-min+1))+min
		if newRand not in ret and newRand not in except
			ret.push newRand
	ret.sort()
	ret
#是否查找下一页
findNext = on
#找id
searchUser = (response,again=on)->
	$allBox = $ '.tb-content .item-box'
	i = 0
	sellerBoxIndex = -1
	itemBox = []
	#是否是天猫
	isTmall = (box)->
		$('.icon-service-tianmao',box).size()>0
	$allBox.each ->
		if $('.seller a',this).html() == response.sellername
			if isTmall this
				alert '被刷店是天猫，请手动刷单'
				return off
			if sellerBoxIndex >= 0 #对于一个页面存在多个的情况，只选第一个
				return on
			sellerBoxIndex = i
		if isTmall this
			return on
		itemBox[i++] = this
	if sellerBoxIndex < 0
		if again
			setTimeout(->
				searchUser response,off
			,10000)
		else
			currentPage = $('.pagination span.page-cur').html()
			nextPageDom = $('.page-siblings .page-next')[0]
			if 0 < currentPage < 4 and nextPageDom and findNext
				simulateClick nextPageDom
			else
				alert '前4页都没有找到'
	else
		preItemIndex = []
		preNeedNum = response.enterNo-1 #前面需要的个数
		preExistNum = sellerBoxIndex #实际前面存在的个数
		endItemIndex = []
		endNeedNum = response.compare-response.enterNo+1 #后面需要的位置数
		endExistNum = itemBox.length-sellerBoxIndex-1 #后面实际存在的个数
		if preNeedNum <= preExistNum #在前面的数量够
			#先排前面
			preItemIndex = randIndex(0,sellerBoxIndex-1,preNeedNum)
			if endNeedNum <= endExistNum #后面位置够
				endItemIndex = randIndex(sellerBoxIndex+1,itemBox.length-1,endNeedNum)
			else #后面位置不够
				endItemIndex = (v for v in [(sellerBoxIndex+1)..(itemBox.length-1)]) #后面的全放进去
				#从前面再放入几个
				endItemIndex = endItemIndex.concat(randIndex(0,sellerBoxIndex-1,endNeedNum-endExistNum,preItemIndex))
		else
			#前面位置不够
			preItemIndex = randIndex 0,preNeedNum,preNeedNum,[sellerBoxIndex] #前面的项除目标以外的都放入
			endItemIndex = randIndex(preNeedNum+1,itemBox.length-1,endNeedNum)
		#点击
		time = 0
		clickImg = (indexes,second)->
			for index in indexes
				dom = itemBox[index]
				((dom,time)->
					setTimeout(->
						img = $('.pic-box a img',dom)
						simulateClick img[0],{ctrl:on}
					,time)
				)(dom,time)
				console.debug "time is #{time}"
				time += second
				console.debug "time end is #{time}"

		#浏览前货比
		clickImg preItemIndex, response.compareTime*65000
		#浏览主宝贝
		clickImg [sellerBoxIndex],response.mainWatchTime*65000
		#浏览后货比
		clickImg endItemIndex,response.compareTime*65000
		#开始浏览其它宝贝
		setTimeout( ->
			console.debug 'start view other goods',new Date().toLocaleTimeString()
			startViewOtherMsg =
				dispatch:
					startViewOther:1
			chrome.extension.sendRequest startViewOtherMsg,(response)->
				console.log response
		,time)
		#发布浏览完成
		setTimeout( ->
			console.debug 'order now',new Date().toLocaleTimeString()
			compeletMsg =
				dispatch:
					firstViewComplete:1
			chrome.extension.sendRequest compeletMsg, (response)->
				console.log response
		,time+(response.otherGoodsNum*response.otherGoodsTime+response.indexWatchTime)*65*1000)
#先得到信息
chrome.extension.sendRequest {sellername:1,compare:1,enterNo:1,compareTime:1,otherGoodsTime:1,mainWatchTime:1,otherGoodsNum:1,otherGoodsNum:1,indexWatchTime:1}, (response)->
	if response and response.buy and response.buy.sellername
		document.body.scrollTop += 1600
		setTimeout( ->
			searchUser response.buy
		,5000)
		$(document).keyup (e)->
			if 78==e.keyCode and e.altKey
				unless findNext
					searchUser response.buy,off
				findNext = !findNext
